﻿using System.Diagnostics;
using System.Net;
using System.Text;
using Telex;

namespace test1;



class Program
{
    private static string server_host = "zz:jianjie";
    private static string client_host = "zz:goutuizi";


    private static byte[] req = Encoding.UTF8.GetBytes("请求=------哈哈哈环境科技");
    private static byte[] res = Encoding.UTF8.GetBytes("响应=------一月又一月晕");
    
    public static async Task Main()
    {
        
        var sHost = new ServerBuilder()
                                        .ConfigureSecretkey("11")
                                        .AddSendProtocol(10, SendFunc)
                                        .AddCallProtocol(90, CallFunc)
                                        .Build();
        await sHost.BindAsync(IPEndPoint.Parse("127.0.0.1:20001"), new RpcHost("zz", "jianjie"));

        
        var cHost = new ClientBuilder()
                                        .ConfigureSecretkey("11")
                                        .AddSendProtocol(10, SendFunc)
                                        .AddCallProtocol(90, CallFunc)
                                        .ConfigureChannelActive((host, kind) =>
                                        {
                                            return ValueTask.CompletedTask;
                                        })
                                        .ConfigureChannelInactive((host, kind) =>
                                        {
                                            return ValueTask.CompletedTask;
                                        })
                                        .Build();
        var remHost = await cHost.ConnectAsync(IPEndPoint.Parse("127.0.0.1:20001"), 10, new RpcHost("zz", "goutuizi"));
        Console.WriteLine("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU>>" + remHost);

        bool isBool = true;
        var channel = cHost.GetChannel(remHost);
        // var task = Task.Run(async () => 
        // {
        //     while (true)
        //     {
        //         await Task.Delay(1);
        //         
        //     }
        // });


        Guid.NewGuid();
        
        while (true)
        {
            var str = Console.ReadLine();
            if (!string.IsNullOrEmpty(str))
            {
                isBool = false;
                break;
            }

            channel.Send(10, req);
            // channel.Call(90, req, (response, opcode) =>
            // {
            //     Console.WriteLine("响应>>data>>" + Encoding.UTF8.GetString(response) + "    opcode>>" + opcode);
            // });
        }

        Console.ReadKey();
    }
    
    
    private static void CallFunc(RpcHost host, uint protocolid, byte[] request, CallResponseDelegate func)
    {
        Console.WriteLine("CallFunc>>收到>" + Encoding.UTF8.GetString(request));
        func.Invoke(res, 200);
    }
    
    private static void SendFunc(RpcHost host, uint protocolid, byte[] request)
    {
        Console.WriteLine("SendFunc>>收到>" + Encoding.UTF8.GetString(request));
    }
}
